import React from 'react';
import ReactDom from 'react-dom';
import './global.less'
import { AppContainer} from 'react-hot-loader';
import {BrowserRouter, Route, Switch } from 'react-router-dom';
import route from './route';
import { cookie } from './utils/cookie';

// cookie.set('csrftoken', '2YsBEAB7ygclFVR6zw3GmK4VStDfhjGr');
// cookie.set('sessionid', 'hnpw48jxh0vbmfpx00b3sveogw54k2ok');

/**
 * switch 匹配一个
 * switch + exact 精确匹配第一个
 */

const renderRouter = (item) => (
    <Route 
        key={item.path} 
        exact={item.exact !== false} 
        path={item.path} 
        component={item.component}
    ></Route>
)
const App = () => {
    return (
    <AppContainer>
       <BrowserRouter>
        <Switch> 
            {
                route.map(item => {
                    if(item.children){
                        const Layout = item.children.layout;
                        item.component = () => (
                            <Layout key={item.path}>
                            {
                                item.children.routes.map(children => {
                                    return renderRouter(children);
                                })
                            } 
                            </Layout>
                        )
                    }
                    return renderRouter(item);
                })
            }
        </Switch>
       </BrowserRouter>
    </AppContainer>
    )
}

ReactDom.render(
   <App/>
    ,
    document.getElementById('app')
)

if (module.hot) {
    module.hot.accept(() => {
      ReactDom.render(
        <App/>,
        document.getElementById('app')
      )
    })
  }
  